package pc.system;

import java.sql.*;
import javax.servlet.*;
import javax.servlet.jsp.JspWriter;
import pc.util.*;

public class Response extends Actions
{
  //private Actions owner = null;
  private String file_out = null;
  private JspWriter out = null;

  Response(Actions owner)
  {
    //this.owner = owner;
    super(owner);
  }

  public void setResponse(ServletResponse response,JspWriter out)
  {
    this.out = out;
  }

  public void print(String s)
  {
    try {
      if (out != null) out.println(s);
    }  catch (java.io.IOException e) {
      System.out.println(e.getMessage());
    }
  }

  public void println(String s)
  {
    print(s + "\r\n");
  }

  public void newWindow(String page)
  {
    print("<script language=" + '"' + "javascript" + '"' + ">");
    print("window.open(" + '"' + page + '"' + ");");
    print("</script>");
  }

/*
  public void forwardTo(String url)
  {
    print("<script language='javascript'>");
    print("document.location.href = '" + url + "'");
    print("</script>");
  }
*/
  public void forwardTo(String uid,String page)
  {
    print("<form  name='formForward' id='formForward' method='post' + action='" + page + "'>");
    print("    <input type='hidden' name='uid' id='uid' value='" + uid + "'>" );
    print("</form>");
    print("<script language='javascript'>");
    print("    document.formForward.submit()");
    print("</script>");
 }

  public void setSelected(String select,String value)
  {
    print("<script language='javascript'>");
    print("   setSelected('" + select + "','" + value + "')");
    print("</script>");
  }

  public void setSelected(String select,int value)
  {
   setSelected(select,"" + value);
  }

  public void setSelected(String select,long value)
  {
   setSelected(select,"" + value);
  }

  public void setSelected(String select,java.sql.Date value)
  {
    if (value == null) value = Utils.nullDate;
   setSelected(select,"" + value);
  }

  public void setSelected(String select)
  {
    String value = getRequest().getValue(select);
    if (value == null) value = "0";
    setSelected(select,value);
  }

  public void set_Selected(String select)
  {
    String value = getFilter().getStringValue(select);
    if (value == null || value.trim().length()==0) value = "0";
    setSelected(select,value);
  }

  public void setOptionAjax(ResultSet rs, String id, String name)
  {
    //if (rs == null) return;\
    Record rc = new Record(rs);
    rc.beforeFirst();
    while (rc.next()) {
      String s = "<option value = '" + rc.getString(id) + "'>" + rc.getString(name) + "</option>";
      print(s);
    }
    rc.close();
  }
  
  public void setOption(ResultSet rs, String id, String name)
  {
    //if (rs == null) return;\
    Record rc = new Record(rs);
    rc.beforeFirst();
    while (rc.next()) {
      String s = "<option value = '" + rc.getString(id) + "'>" + rc.getString(name) + "</option>";
      print(s);
    }
    rc.close();
  }

  public void set_Option(ResultSet rs)
  {
    Record rc = new Record(rs);
    rc.beforeFirst();
    while (rc.next()) {
      String color = getColor(rc.getIntValue("completed"));
      String s = "<option " + color + " value = '" + rc.getLong("id") + "'>" + rc.getString("name") + "</option>";
      print(s);
    }
    rc.close();
  }


  public void setOption(ResultSet rs, String id)
  {
    setOption(rs,id,"name");
  }

  public void setOptionAjax(ResultSet rs)
  {
    setOptionAjax(rs,"id","name");
  }
  
  public void setOption(ResultSet rs)
  {
    setOption(rs,"id","name");
  }

  public void setOptionX(ResultSet rs)
  {
    //if (rs == null) return;
    Record rc = new Record(rs);
    rc.beforeFirst();
    while (rc.next()) {
      String color = getColor(rc.getIntValue("status"));
      String style = "style='color:" + color + "'";
      String s = "<option " + style + " value = '" + rc.getString("id") + "'>" + rc.getString("name") + "</option>";
      print(s);
    }
    rc.close();
  }

  public void setOption(String table_name,String field_name)
  {
    setOption(new Table(this,table_name).getList(field_name));
  }

  public void setOption(String table_name,String field_name,int active)
  {
    setOption(new Table(this,table_name).getList(field_name,active));
  }

  public void setOption(String table_name)
  {
    setOption(table_name,"name");
  }

  public void setOption(String table_name,int active)
  {
    setOption(table_name,"name",active);
  }

  public void setReport(String file_out)
  {
    this.file_out = file_out;
  }

  public void viewReport(String report)
  {
    if (report == null) return;
    int browser = getRequest().getIntValue("browser");
    String target = (browser != 0) ? "" : "_blank";
    print("<form name='viewReport' action='" + report + "' method='POST' target='" + target + "'>");
    print("</form>");
    print("<script language='JavaScript'>");
    print("    document.viewReport.submit();");
    print("</script>");
  }

  public void viewReport()
  {
    viewReport(file_out);
    setReport(null);
  }

}
